#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>

#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>

#define NEW_LED_DRV     1
#define NEW_LED_NAME    "newled"

#define LEDON   1
#define LEDOFF  0

#define PA_CFG1_ADDR    (0x01c20804)
#define PA_DATA_ADDR    (0x01c20810)

static void __iomen *PA_CFG1;
static void __iomen *PA_DATA;

struct new_led{
	dev_t devid;			/* 设备号 	 */
	struct cdev cdev;		/* cdev 	*/
	struct class *class;		/* 类 		*/
	struct device *device;	/* 设备 	 */
	int major;				/* 主设备号	  */
	int minor;				/* 次设备号   */
};

struct new_led newleddev;

//香橙派灯是高电平亮，低电平灭
void led_switch(u8 sta)
{
    u32 val = 0;
	if(sta == LEDON) {
		val = readl(PA_DATA);
		//val &= ~(1 << 15);
		val |= (1 << 15);//置1	
		writel(val, PA_DATA);
	}else if(sta == LEDOFF) {
		val = readl(PA_DATA);
		//val |= (1 << 15);
		val &= ~(1 << 15);//置0	
		writel(val, PA_DATA);
	}	
}

static int led_open(struct inode *inode, struct file *filp)
{
	filp->private_data = &newleddev; /* 设置私有数据 */
	return 0;
}

static ssize_t led_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
	int retvalue;
	unsigned char databuf[1];
	unsigned char ledstat;

	retvalue = copy_from_user(databuf, buf, cnt);
	if(retvalue < 0) {
		printk("kernel write failed!\r\n");
		return -EFAULT;
	}

	ledstat = databuf[0];		/* 获取状态值 */

	if(ledstat == LEDON) {	
		led_switch(LEDON);		/* 打开LED灯 */
	} else if(ledstat == LEDOFF) {
		led_switch(LEDOFF);	/* 关闭LED灯 */
	}
	return 0;
}

static int led_release(struct inode *inode, struct file *filp)
{
	return 0;
}

static struct file_operations newleddev_fops = {
	.owner = THIS_MODULE,
	.open = led_open,
	//.read = led_read,
	.write = led_write,
	.release = 	led_release,
};

static int __init led_init(void)
{
    int retvalue = 0;
	u32 val = 0;

    PA_CFG1=ioremap(PA_CFG1_ADDR,4);
    PA_DATA=ioremap(PA_DATA_ADDR,4);

    val=readl(PA_CFG1);
    val&=~(1<<28);
    val|=(1<<28);
    writel(val,PA_CFG1);

    /*注册字符设备驱动*/
    if(newleddev.major){
        newleddev.devid = MKDEV(newleddev.major,0);
        register_chrdev_region(newleddev.devid, NEW_LED_DRV , NEW_LED_NAME);
    }else{
        alloc_chrdev_region(&newleddev.devid, 0,NEW_LED_DRV , NEW_LED_NAME);
        newleddev.major = MAJOR(newleddev.devid);
        newleddev.minor = MINOR(newleddev.devid);
    }
    printk("newchrled major=%d,minor=%d \r\n",newleddev.major,newleddev.minor);

    newleddev.cdev.owner = THIS_MODULE;
    cdev_init(&newleddev.cdev , &newleddev_fops);

    cdev_add(&newleddev.cdev, newleddev.devid, NEW_LED_DRV);

    newleddev.class = class_create(THIS_MODULE, NEW_LED_NAME);
    if (IS_ERR(newleddev.class)){
        return PTR_ERR(newleddev.class);
    }

    newleddev.device = device_create(newleddev.class, NULL, newleddev.devid, NULL, NEW_LED_NAME);
    if (IS_ERR(newleddev.device)) {
		return PTR_ERR(newleddev.device);
	}
	
	return 0;
}

static void __exit led_exit(void)
{
    iounmap(PA_CFG1);
    iounmap(PA_DATA);

    //unregister_chrdev(LED_MAJOR, LED_NAME);
    cdev_del(&newleddev.cdev);
    unregister_chrdev_region(newleddev.devid, NEW_LED_DRV);

    device_destroy(newleddev.class, newleddev.devid);
    class_destroy(newleddev.class);
}

module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("HRX");
